#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""文件操作工具模块
提供文件读写、目录操作等功能"""

import os
import json
from .logger import Logger

class FileUtils:
    """文件操作工具类
    提供文件读写、目录操作等功能"""
    
    @staticmethod
    def read_file(file_path):
        """读取文件内容
        
        Args:
            file_path: 文件路径
            
        Returns:
            str: 文件内容，失败返回None
        """
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                return f.read()
        except Exception as e:
            Logger.log_error(f"读取文件失败: {file_path}, 错误: {str(e)}")
            return None
    
    @staticmethod
    def write_file(file_path, content):
        """写入文件内容
        
        Args:
            file_path: 文件路径
            content: 要写入的内容
            
        Returns:
            bool: 是否写入成功
        """
        try:
            # 确保目录存在
            dir_path = os.path.dirname(file_path)
            if dir_path and not os.path.exists(dir_path):
                os.makedirs(dir_path)
            
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            return True
        except Exception as e:
            Logger.log_error(f"写入文件失败: {file_path}, 错误: {str(e)}")
            return False
    
    @staticmethod
    def read_json(file_path):
        """读取JSON文件
        
        Args:
            file_path: 文件路径
            
        Returns:
            dict: JSON数据，失败返回None
        """
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                return json.load(f)
        except Exception as e:
            Logger.log_error(f"读取JSON文件失败: {file_path}, 错误: {str(e)}")
            return None
    
    @staticmethod
    def write_json(file_path, data):
        """写入JSON文件
        
        Args:
            file_path: 文件路径
            data: 要写入的JSON数据
            
        Returns:
            bool: 是否写入成功
        """
        try:
            # 确保目录存在
            dir_path = os.path.dirname(file_path)
            if dir_path and not os.path.exists(dir_path):
                os.makedirs(dir_path)
            
            with open(file_path, 'w', encoding='utf-8') as f:
                json.dump(data, f, ensure_ascii=False, indent=4)
            return True
        except Exception as e:
            Logger.log_error(f"写入JSON文件失败: {file_path}, 错误: {str(e)}")
            return False
    
    @staticmethod
    def file_exists(file_path):
        """检查文件是否存在
        
        Args:
            file_path: 文件路径
            
        Returns:
            bool: 文件是否存在
        """
        return os.path.isfile(file_path)
    
    @staticmethod
    def create_directory(dir_path):
        """创建目录
        
        Args:
            dir_path: 目录路径
            
        Returns:
            bool: 是否创建成功
        """
        try:
            if not os.path.exists(dir_path):
                os.makedirs(dir_path)
            return True
        except Exception as e:
            Logger.log_error(f"创建目录失败: {dir_path}, 错误: {str(e)}")
            return False